# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : facial_editor.py
# Date      : 2019/8/22 
# IDE       : PyCharm
# Version   : 1.0.1
# -------------------------------
import traceback

import maya.cmds as mc
import maya.mel as mel

import configs.constants.facial_constant as fc
import configs.constants.global_constant as gc
import configs.constants.suffix_constant as sc

reload(fc)
reload(gc)
reload(sc)

import scripts.commonCmds as con
import scripts.deformerCmds as dc
import editors.editor as editor
reload(editor)
reload(dc)

import rig.facial.fTemplate as ftmp

import rig.facial.fCore as fcore
import rig.facial.fBrow as fbrow
import rig.facial.fEye as feye
import rig.facial.fMouth as fmouth
import rig.facial.fNose as fnose
import rig.facial.fEar as fear
import rig.facial.fTongue as ftongue
import rig.facial.fStretch as fstretch

def showEditor():
    FacialEditor().win()

class FacialEditor(editor.Editor):
    def __init__(self):
        super(FacialEditor,self).__init__()
        
    def win(self):
        _win = self.selfWindow()
        mc.window(_win,e=True,wh=[350,600],s=False)
        mc.menuBarLayout(bgc=self.rgbConvert(118, 137, 143))
        mc.menu("File", )
        mc.menuItem()
        mc.setParent("..")
        column = mc.columnLayout(adj=True,)
        mc.scrollLayout(h=600,w=350,vsb=True)
        main_column = mc.columnLayout(adj=True,w=340)
        
        self.preference_frame()
        self.brow_frame()
        self.eye_frame()
        self.mouth_frame()
        self.nose_frame()
        self.ear_frame()
        self.tongue_frame()
        self.stretch_frame()
        self.other_frame()
        
        mc.setParent("..")
        mc.setParent("..")
        mc.setParent("..")
        self.copyright()

        mc.showWindow(_win)
        return
    
    def preference_frame(self):
        
        frame = mc.frameLayout(con.selfFuncName(),l="Preference Setting ...",cll=True,cl=True)
        # mc.gridLayout(nc=2,cw=174,bgc=self.rgbConvert(6,82,121))
        mc.columnLayout(adj=True)
        
        # taglist = [
        #     ["Head","Tongue"],
        #     ["L_brow", "R_brow"],
        #     ["L_innEyeball","R_innEyeball"],
        #     ["L_outEyeball", "R_outEyeball"],
        #     ["Up_teeth","Lo_teeth"]
        # ]
        taglist = fc.taglist
        
        for i,tag in enumerate(taglist):
        
            self.control_tfbg(tag,i)
        mc.separator()
        mc.button(l=" - Preference Setting - ",c=lambda *args:self.btn_preference_setting()
                  ,bgc=self.rgbConvert(243, 104, 56))
        
        mc.setParent("..")
        mc.setParent("..")

        return
    def control_tfbg(self,tag,count):
        
        cw = [72*2,62*2,43*2]
        
        pullName1 = tag+"textFieldButtonGrp"
        mc.textFieldButtonGrp(pullName1,l=tag+" :", bl="Load", cw3=cw,ed=False,adj=3,
                              bc=lambda *args: self.btn_preference_load(pullName1,tag))
        if not count%2:
            mc.textFieldButtonGrp(pullName1,e=True,bgc=self.rgbConvert(243, 104, 56),)
        nn = con.lname(tag,"f"+sc.sGeo)
        if mc.objExists(nn):
            mc.textFieldButtonGrp(pullName1, e=True, tx=nn)
        
        return
    
    def btn_preference_load(self,pullName,tag):
        
        sel = mc.ls(sl=True)
        if sel:
            nn = con.lname(tag,"f"+sc.sGeo)
            mc.textFieldButtonGrp(pullName,e=True,tx=nn)
            mc.rename(sel[0],nn)
        
        return
    def btn_preference_setting(self):
        reload(fcore)
        fcore.FacialCore().preference()
        
        return
    
    def brow_frame(self):
        
        frame = mc.frameLayout(con.selfFuncName(),l="Brow Setting ...",cll=True,cl=True)
        cw = [90,62*3,43*2]
        column = mc.columnLayout(adj=True)
        pullName1 = con.lname(fc.brow,fc.main,sc.sCrv)
        mc.textFieldButtonGrp(pullName1,l=pullName1+" :", bl="Load", cw3=cw, ed=False,adj=3,
                              bc=lambda *args: self.btn_brow_mainCrv(pullName1))

        tmp_Brow_Main_crv = "*_tmp_Brow_Main_crv"
        if mc.objExists(tmp_Brow_Main_crv):
            mc.textFieldButtonGrp(pullName1, e=True,tx=tmp_Brow_Main_crv)
        
        pullName2 = con.lname(fc.brow,"slider",sc.sCrv)
        mc.textFieldButtonGrp(pullName2,l=pullName2+" :", bl="Load", cw3=cw,ed=False,adj=3,
                              bc=lambda *args: self.btn_brow_sliderCrv(pullName2))
        tmp_Brow_Main_crv = "tmp_Brow_slider_Main_crv"
        if mc.objExists(tmp_Brow_Main_crv):
            mc.textFieldButtonGrp(pullName2, e=True,tx=tmp_Brow_Main_crv)
        # mc.button(l="Test",c=lambda *args:self.btn_test())
        mc.button(l=" - Create - ",c=lambda *args:self.btn_brow_create(), bgc=self.rgbConvert(243, 104, 56), )
        mc.setParent("..")
        
        mc.setParent("..")
        
        return
    
    def btn_brow_mainCrv(self,pullName):
        reload(ftmp)
        ftc = ftmp.BrowTemplate()
        bmc = ftc.brow_mainCrv()
        
        mc.textFieldButtonGrp(pullName,e=True,tx=bmc)
        
        return
    
    def btn_brow_sliderCrv(self,pullName):
        reload(ftmp)
        ftc = ftmp.BrowTemplate()
        bmc = ftc.brow_slider()
    
        mc.textFieldButtonGrp(pullName, e=True, tx=bmc)
    
        return
    def btn_brow_create(self):
    
        reload(fbrow)
        brow = fbrow.Brow()
        # brow.static_driver()
        brow.create()
        
        return

    def eye_frame(self):
    
        frame = mc.frameLayout(con.selfFuncName(), l="Eye Setting ...", cll=True,cl=True)
        cw = [72 * 2, 62 * 2, 43 * 2]
        column = mc.columnLayout(adj=True)

        mc.button(l=" - Eyeball center point - ", c=lambda *args: self.btn_eyeball_centerPoint())
        mc.button(l=" - Eyeball aim point - ", c=lambda *args: self.btn_eyeball_aimPoint())
        mc.button(l=" - Eyelid inner point - ", c=lambda *args: self.btn_eyelid_inn())
        mc.button(l=" - Eyelid outer point - ", c=lambda *args: self.btn_eyelid_out())
        
        mc.separator()
        text_format = "    {:<40}"

        row = mc.rowLayout(nc=10)
        pullName1 = con.lname(fc.eyelid, "skin")
        mc.text(self.text(text_format.format(fc.eyelid + " skin jot number : ")))
        mc.intField(pullName1, w=30, v=15)
        mc.button("Reset", w=50, c=lambda *args: self.btn_eyelid_skinReset(pullName1))
        mc.setParent("..")

        row = mc.rowLayout(nc=10)
        pullName2 = con.lname(fc.eyelid, "ctrl")
        mc.text(self.text(text_format.format(fc.eyelid + " ctrl jot number : ")))
        mc.intField(pullName2, w=30, v=9)
        mc.button("Reset", w=50, c=lambda *args: self.btn_eyelid_ctrlReset(pullName2))
        mc.setParent("..")
        mc.separator()

        # mc.button(l=" - Eyelid upper point - ", c=lambda *args: self.btn_eyelid_up())
        # mc.button(l=" - Eyelid lower point - ", c=lambda *args: self.btn_eyelid_lo())
        # mc.separator()
        mc.button(l=" - Creat -",bgc=self.rgbConvert(243, 104, 56),c=lambda *args:self.btn_eyeCreate())
        mc.separator()

        mc.button(" - Socket Upper Line - ", w=50, c=lambda *args: self.btn_eyeSocket_upline())
        mc.button(" - Socket Lower Line - ", w=50, c=lambda *args: self.btn_eyeSocket_loline())

        row = mc.rowLayout(nc=10)
        pullName3 = con.lname(fc.eyeSocket)
        mc.text(self.text(text_format.format(fc.eyeSocket + " number : ")))
        mc.intField(pullName3, w=30, v=5)
        mc.button("Reset", w=50, c=lambda *args: self.btn_eyeSocket_reset(pullName3))
        mc.setParent("..")
        mc.separator()
        mc.button(l=' - Socket Create - ',bgc=self.rgbConvert(243, 104, 56),c=lambda *args:self.btn_eyeSocket_create())
        mc.setParent("..")
    
        mc.setParent("..")
    
        return
    
    def btn_eyeball_centerPoint(self):
        reload(ftmp)
        ftc = ftmp.EyeTemplate()
        ftc.eyeball_center()
        
        return

    def btn_eyeball_aimPoint(self):
        reload(ftmp)
        ftc = ftmp.EyeTemplate()
        ftc.eyeball_aim()
    
        return

    def btn_eyelid_inn(self):
        reload(ftmp)
        ftc = ftmp.EyeTemplate()
        ftc.eyelid_inn()
    
        return

    def btn_eyelid_out(self):
        reload(ftmp)
        ftc = ftmp.EyeTemplate()
        ftc.eyelid_setting()
    
        return
    
    def btn_eyelid_skinReset(self,pullname):
        
        number = mc.intField(pullname,q=True,v=True)
        
        reload(ftmp)
        ftc = ftmp.EyeTemplate()
        ftc.eyelid_skin(number)
        return

    def btn_eyelid_ctrlReset(self, pullname):
    
        number = mc.intField(pullname, q=True, v=True)
    
        reload(ftmp)
        ftc = ftmp.EyeTemplate()
        ftc.eyelid_ctrl(number)
        return
    def btn_eyeSocket_upline(self):
        reload(ftmp)
        ftmp.EyeTemplate().eyeSocket_line(fc.up)
        return
    
    def btn_eyeSocket_loline(self):
        reload(ftmp)
        ftmp.EyeTemplate().eyeSocket_line(fc.lo)
        return
    
    def btn_eyeSocket_reset(self, pullname):
    
        number = mc.intField(pullname, q=True, v=True)
    
        reload(ftmp)
        ftc = ftmp.EyeTemplate()
        ftc.eyeSocket_number(number)
        return
    
    def btn_eyeCreate(self):
        reload(feye)
        fe = feye.Eye()
        fe.create()
        
        return
    
    def btn_eyeSocket_create(self):
        reload(feye)
        fe = feye.Eye()
        fe.socket_create()
        
        return

    def mouth_frame(self):
    
        frame = mc.frameLayout(con.selfFuncName(), l="Mouth Setting ...", cll=True,cl=True)
        cw = [72 * 2, 62 * 2, 43 * 2]
        column = mc.columnLayout(adj=True)
        
        mc.button(l=" - Jaw Point - ",c=lambda *args:self.btn_mouth_jaw())
        mc.separator()

        mc.button(l=" - Upper lip - ",c=lambda *args:self.btn_mouth_lipCurve(fc.up))
        mc.button(l=" - Lower lip - ",c=lambda *args:self.btn_mouth_lipCurve(fc.lo))
        mc.separator()
        
        text_format = "    {:<40}"
        
        row = mc.rowLayout(nc=10,en=True)
        pullName3 = con.lname(fc.lip)
        mc.text(self.text(text_format.format(fc.lip + " Skin number : ")))
        mc.intField(pullName3, w=30, v=9)
        mc.button("Reset", w=50, c=lambda *args: self.btn_mouth_lipJotNumber(pullName3))
        mc.setParent("..")
        
        row = mc.rowLayout(nc=10,en=True)
        pullName2 = con.lname(fc.lip+"1")
        mc.text(self.text(text_format.format(fc.lip + " Ctrl number : ")))
        mc.intField(pullName2, w=30, v=9)
        mc.button("Reset", w=50, c=lambda *args: self.btn_mouth_lipCtrlNumber(pullName2))
        mc.setParent("..")
        
        mc.separator()
        mc.button(l=" - Lip Slider - ",c=lambda *args:self.btn_mouth_slider())
        mc.separator()
        mc.button(l=" - Create - ",c=lambda *args:self.btn_mouth_create(),bgc=self.rgbConvert(243, 104, 56))

        mc.setParent("..")
        
        mc.setParent("..")

        return
    
    def btn_mouth_jaw(self):
        reload(ftmp)
        ftc = ftmp.MouthTemplate()
        ftc.jaw_point()
        return
    
    def btn_mouth_lipCurve(self,tag):
        reload(ftmp)
        ftc=ftmp.MouthTemplate()
        ftc.lip_curve(tag)
        return
    
    def btn_mouth_lipJotNumber(self,pullname):
        number = mc.intField(pullname, q=True, v=True)
        
        reload(ftmp)
        ftc=ftmp.MouthTemplate()
        ftc.lip_skin_number(number)
        return

    def btn_mouth_lipCtrlNumber(self, pullname):
        number = mc.intField(pullname, q=True, v=True)
    
        reload(ftmp)
        ftc = ftmp.MouthTemplate()
        ftc.lip_ctrl_number(number)
        return
    
    def btn_mouth_slider(self):
        reload(ftmp)
        ftc = ftmp.MouthTemplate()
        ftc.lip_slider()
        
        return
        
    def btn_mouth_create(self):
        try:
            reload(fmouth)
            fm = fmouth.Mouth()
            fm.create()
        except :
            traceback.print_exc()
        return

    def nose_frame(self):
    
        frame = mc.frameLayout(con.selfFuncName(), l="Nose Setting ...", cll=True, cl=True)
        cw = [72 * 2, 62 * 2, 43 * 2]
        column = mc.columnLayout(adj=True)
        mc.button(l=" - Nose Root - ",c=lambda *args:self.btn_nose_root())
        mc.button(l=" - Nose Tip - ",c=lambda *args:self.btn_nose_tip())
        mc.separator()
        mc.button(l=" - create - ",c=lambda *args:self.btn_nose_create(),bgc=self.rgbConvert(243, 104, 56))
        mc.setParent("..")
        mc.setParent("..")

        return
    def btn_nose_root(self):
        reload(ftmp)
        fn = ftmp.NoseTemplate()
        fn.noseRoot()
        return
    def btn_nose_tip(self):
        reload(ftmp)
        fn = ftmp.NoseTemplate()
        fn.noseTip()
        return
    def btn_nose_create(self):
        reload(fnose)
        fnose.Nose().create()
        
        return

    def ear_frame(self):
    
        frame = mc.frameLayout(con.selfFuncName(), l="Ear Setting ...", cll=True, cl=True)
        cw = [72 * 2, 62 * 2, 43 * 2]
        column = mc.columnLayout(adj=True)
        mc.button(l=" - Ear Root - ", c=lambda *args: self.btn_ear_root())
        mc.button(l=" - Ear End - ", c=lambda *args: self.btn_ear_end())
        text_format = "    {:<40}"
        row = mc.rowLayout(nc=10, en=True)
        pullName2 = con.lname(fc.ear)
        mc.text(self.text(text_format.format(fc.ear + " number : ")))
        mc.intField(pullName2, w=30, v=2)
        mc.button("Reset", w=50, c=lambda *args: self.btn_earNumber(pullName2))
        mc.setParent("..")
        mc.separator()
        mc.button(l=" - create - ", c=lambda *args: self.btn_ear_create(), bgc=self.rgbConvert(243, 104, 56))
        mc.setParent("..")
        mc.setParent("..")
    
        return
    
    def btn_ear_root(self):
        reload(ftmp)
        ft = ftmp.EarTemplate()
        ft.earRoot()
        return
    def btn_ear_end(self):
        reload(ftmp)
        ft = ftmp.EarTemplate()
        ft.earEnd()
        return

    def btn_earNumber(self, pullname):
        number = mc.intField(pullname, q=True, v=True)
    
        reload(ftmp)
        ftc = ftmp.EarTemplate()
        ftc.earNumber(number)
        return
    def btn_ear_create(self):
        reload(fear)
        fe = fear.Ear()
        fe.create()
        return

    def tongue_frame(self):
    
        frame = mc.frameLayout(con.selfFuncName(), l="Tongue Setting ...", cll=True, cl=True)
        cw = [72 * 2, 62 * 2, 43 * 2]
        column = mc.columnLayout(adj=True)
        mc.button(l="- Import Tongue Template -",c=lambda *args:self.btn_tongue_import())
        text_format = "    {:<40}"
        row = mc.rowLayout(nc=10, en=True)
        pullName2 = con.lname(fc.tongue)
        mc.text(self.text(text_format.format(fc.tongue + " number : ")))
        mc.intField(pullName2, w=30, v=5)
        mc.button("Reset", w=50, c=lambda *args: self.btn_tongueNumber(pullName2))
        mc.setParent("..")
        mc.separator()
        mc.button(l="- Create - ",bgc=self.rgbConvert(243, 104, 56),c=lambda *args:self.btn_tongue_create())

        mc.setParent("..")
        mc.setParent("..")
        return
    def btn_tongue_import(self):
        reload(ftmp)
        ftmp.TongueTemplate().import_template()
        
        return
    def btn_tongueNumber(self, pullname):
        number = mc.intField(pullname, q=True, v=True)
    
        reload(ftmp)
        ftc = ftmp.TongueTemplate()
        ftc.tongueNumber(number)
        return
    def btn_tongue_create(self):
        reload(ftongue)
        fe = ftongue.Tongue()
        fe.create()
        return

    def stretch_frame(self):
    
        frame = mc.frameLayout(con.selfFuncName(), l="Stretch Setting ...", cll=True, cl=True)
        cw = [72 * 2, 62 * 2, 43 * 2]
        column = mc.columnLayout(adj=True)
        
        mc.button(l="- Import Streth Template -",c=lambda *args:self.btn_stretch_import())
        mc.separator()
        mc.button(l="- Create - ",bgc=self.rgbConvert(243, 104, 56),c=lambda *args:self.btn_stretch_create())
        mc.setParent("..")
        mc.setParent("..")
        return

    def btn_stretch_import(self):
        reload(ftmp)
        ftmp.StretchTemplate().import_template()
        return
    
    def btn_stretch_create(self):
        reload(fstretch)
        fstretch.Stretch().create()
        
        return
    
    def other_frame(self):
    
        frame = mc.frameLayout(con.selfFuncName(), l="Other Setting ...", cll=True, cl=True)
        cw = [72 * 2, 62 * 2, 43 * 2]
        column = mc.columnLayout(adj=True)
        pullName2 = con.lname("select_to_other")
        mc.textFieldButtonGrp(pullName2,l=pullName2+" :", bl="Create", cw3=cw,ed=True,adj=3,
                              bc=lambda *args: self.btn_other(pullName2))
        mc.separator()
        mc.button(l="- SkinCluster To Geometry -",c=lambda *args:self.btn_skinCluster_to_geometry())
        mc.setParent("..")
        mc.setParent("..")
        
    def btn_other(self,pullName):
        tx = mc.textFieldButtonGrp(pullName,q=True,tx=True)
        reload(fcore)
        fcc = fcore.FacialCore("other")
        fcc.transform_init()
        fcc.other_ctr_loc(tx)
        return
    def btn_skinCluster_to_geometry(self):
        try:
            brow_jot=mc.ls("*_Brow_skin_joint_*_jot",fl=True)
            if brow_jot:
                tfbg = con.lname(fc.lf,fc.brow+"textFieldButtonGrp")
                tx = mc.textFieldButtonGrp(tfbg,q=True,tx=True)
                if tx:
                    self.cmd_skincluster_to_geo(brow_jot,tx)
        except:
            traceback.print_exc()
        
        return
    
    def cmd_skincluster_to_geo(self,jot_list,geo):
        _fc = fcore.FacialCore()
        
        fjot = "facial_jot"
        if not mc.objExists(fjot):
            _fc.preference()
        
        skin = dc.findRelatedDeformer(geo)
        print skin
        if skin:
            for jot in jot_list:
                mc.skinCluster(skin,e=True,ai=jot,lw=True)
        
        else:
            print 555
        
        return
    
    
if __name__ == '__main__':
    pass